﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gollie
{
	public class ObjectFormatter
	{
		public static string GetFormattedDate(DateTime dateTime)
		{
			string dayFormat = (dateTime.Date == DateTime.Today ? "'Vandaag'" : dateTime.Date == DateTime.Today.AddDays(-1) ? "'Gisteren'" : "dd-MM-yyyy");
			string timeFormat = (dateTime.TimeOfDay == TimeSpan.Zero ? "" : ", HH:mm:ss");

			return dateTime.ToString(dayFormat + timeFormat);
		}

		public static string GetPercentage(decimal number)
		{
			return string.Format("{0:p0}", number);
		}

		public static string GetFormattedTimeStamp()
		{
			return GetFormattedTimeStamp(DateTime.Now);
		}

		public static string GetFormattedTimeStamp(DateTime dateTime)
		{
			return dateTime.ToString("dd-MMM-yyyy, HH:mm:ss");
		}

		public static string GetFormattedTimeStampNoSeconds(DateTime dateTime)
		{
			return dateTime.ToString("dd-MMM-yyyy, HH:mm");
		}

		public static string GetDateString(DateTime dateTime)
		{
			return dateTime.ToString("dd-MMM-yyyy");
		}

		public static string GetShortDateString(DateTime dateTime)
		{
			return dateTime.ToString("dd-MM-yyyy");
		}

		public static string GetFormattedDateTimeOnly(DateTime dateTime)
		{
			return dateTime.ToString("HH:mm:ss");
		}

		public static string GetDecimal(decimal number)
		{
			return number.ToString("0.00");
		}

		public static string GetCurrencyString(decimal number, bool nulIsEmpty)
		{
			if (nulIsEmpty == false || number != 0)
				return number.ToString("c2");

			return "";
		}

		public static string GetCurrencyString(decimal number)
		{
			return GetCurrencyString(number, false);
		}

		public static string GetCurrencyString(object o, bool nulIsEmpty)
		{
			return GetCurrencyString(o == null ? 0 : (decimal)o, nulIsEmpty);
		}

		public static string GetCurrencyString(object o)
		{
			return GetCurrencyString(o, false);
		}

		public static string GetDurationString(TimeSpan ts)
		{
			if (ts.Hours == 0)
				return String.Format("{0:%d} dagen", ts);

			if (ts.Days == 0)
				return String.Format("{0:%h} uur", ts);

			return string.Format("{0:%d}d, {0:%h}h", ts);
		}

		public static string GetDurationString(int hours)
		{
			TimeSpan ts = new TimeSpan(hours, 0, 0);
			return GetDurationString(ts);
		}

		public static string CurrentIp
		{
			get
			{
				string ip = null;

				if (HttpContext.Current != null)
					if (HttpContext.Current.Request != null)
						if (HttpContext.Current.Request.ServerVariables != null)
							ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

				return ip;
			}
		}
	}
}